3 import java
.awt
.Graphics
;
4 import java
.util
.ArrayList
;
6 import java
.util
.Observable
;
7 import java
.util
.Observer
;
11 import controllers
.SelectionController
;
12 import models
.GraphEdge
;
13 import models
.GraphModel
;
14 import models
.GraphVertex
;
16 public class GraphPanel
extends JPanel
implements Observer
{
17 private static final long serialVersionUID
= 1388592199136335514L;
18 private GraphModel model
;
19 private SelectionController selectionController
;
21 public GraphPanel(GraphModel model
){
22 this.setSize(500, 400);
23 this.setBackground(Color
.BLUE
);
26 GraphVertex a
= new GraphVertex();
27 GraphVertex b
= new GraphVertex();
28 GraphVertex c
= new GraphVertex();
30 GraphEdge edge1
= new GraphEdge(new GraphVertex
[] {a
,b
});
31 GraphEdge edge2
= new GraphEdge(new GraphVertex
[] {b
,c
});
32 GraphEdge edge3
= new GraphEdge(new GraphVertex
[] {c
,a
});
34 model
.addGraph(a
,b
,c
);
35 model
.addEdge(edge1
,edge2
,edge3
);
36 model
.addObserver(this);
39 public void setModel(GraphModel model
){
40 if(this.model
!= null){
41 this.model
.deleteObserver(this);
44 model
.addObserver(this);
45 selectionController
.setModel(model
);
48 int[] calculateOrigenPointOfLine(GraphVertex a
, GraphVertex b
)
50 int[] returnvalue
= new int[2];
51 if(a
.getX() > (b
.getX() + a
.getWidth())){
52 // Rechterzijkant gebruiken
54 returnvalue
[0] = a
.getX();
55 returnvalue
[1] = a
.getY()+ (a
.getHeight() / 2);
57 if(b
.getX() > (a
.getX() + a
.getWidth())){
58 // Linkerzijkant gebruiken
59 returnvalue
[0] = a
.getX()+a
.getWidth();
60 returnvalue
[1] = a
.getY()+ (a
.getHeight() / 2);
62 if(b
.getY() > a
.getY()){
63 // bovenkant gebruiken
64 returnvalue
[0] = a
.getX()+ (a
.getWidth()/2);
65 returnvalue
[1] = a
.getY()+ a
.getHeight();
67 // onderkant gebruiken
68 returnvalue
[0] = a
.getX()+ (a
.getWidth()/2);
69 returnvalue
[1] = a
.getY();
78 model
.addGraph(new GraphVertex());
81 public void deleteSelectedNode()
83 model
.removeGraph(selectionController
.getSelectedVertex());
84 selectionController
.removeSelected();
87 public void paintComponent(Graphics g
)
89 g
.setColor(Color
.white
);
90 g
.fillRect(0, 0, getWidth(), getHeight());
92 GraphVertex
[] objects
= model
.getVertexes();
93 for(int i
=0; i
< objects
.length
; i
++){
94 GraphVertex o
= objects
[i
];
96 g
.setColor(Color
.red
);
98 g
.setColor(Color
.black
);
100 g
.drawRect(o
.getX(), o
.getY(),o
.getWidth(), o
.getHeight());
101 g
.setColor(Color
.black
);
102 g
.drawString(o
.getName(), o
.getX()+10, o
.getY()+25);
104 GraphEdge
[] edges
= model
.getEdges();
105 g
.setColor(Color
.cyan
);
106 for(int i
=0; i
< edges
.length
; i
++){
107 GraphEdge edge
= edges
[i
];
108 GraphVertex
[] vertexes
= edge
.getNodes();
109 int[] origen_1
= calculateOrigenPointOfLine(vertexes
[0], vertexes
[1]);
110 int[] origen_2
= calculateOrigenPointOfLine(vertexes
[1], vertexes
[0]);
111 g
.drawLine(origen_1
[0], origen_1
[1], origen_2
[0], origen_2
[1]);
116 public void update(Observable arg0
, Object arg1
) {
118 System
.out
.println("Refresh @ graphpanel" + arg0
.toString());
121 System
.out
.println("FOUT: onbekend object riep GraphPanel.update aan: " + arg0
.toString());